package cn.stylefeng.guns.modular.rest.hxStorageStock.service.impl;

import cn.stylefeng.guns.modular.hx.storage.entity.HxStorageStock;
import cn.stylefeng.guns.modular.hx.storage.mapper.HxStorageStockMapper;
import cn.stylefeng.guns.modular.hx.storage.model.params.HxStorageStockParam;
import cn.stylefeng.guns.modular.hx.storage.model.result.HxStorageStockResult;
import cn.stylefeng.guns.modular.rest.aop.UserAop;
import cn.stylefeng.guns.modular.rest.hxStorageStock.service.HxAppStorageStockService;
import cn.stylefeng.guns.sys.modular.system.entity.User;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.List;

@Service
public class HxAppStorageStockServiceImpl extends ServiceImpl<HxStorageStockMapper, HxStorageStock> implements HxAppStorageStockService {
    @Override
    public List<HxStorageStockResult> findGoodsListBySpec(HxStorageStockParam hxStorageStockParam, Long deptId, Long operateId) {
        return this.baseMapper.findGoodsListBySpec(hxStorageStockParam, deptId, operateId);
    }

    @Override
    public List<HxStorageStockResult> findKcListBySpec(HxStorageStockParam hxStorageStockParam, Long deptId, Long operateId) {
        return this.baseMapper.findKcListBySpec(hxStorageStockParam, deptId, operateId);
    }

    @Override
    public List<HxStorageStockResult> hxStorageMoveList(HxStorageStockParam hxStorageStockParam, String condition, User user) {
        return this.baseMapper.hxStorageMoveList(hxStorageStockParam, condition, user.getDeptId(), user.getOperateId());
    }

    @Override
    public List<HxStorageStockResult> hxStorageoffShelfList(HxStorageStockParam hxStorageStockParam, String condition, User user) {
        return this.baseMapper.hxStorageoffShelfList(hxStorageStockParam, condition, user.getDeptId(), user.getOperateId());
    }

    @Override
    @Transactional(rollbackFor = Exception.class,propagation= Propagation.REQUIRED,timeout=60)
    public ResponseData updateDetailedDate(HxStorageStockParam hxStorageStockParam) {

        if (ToolUtil.isNotEmpty(hxStorageStockParam.getId())) {
            //查询库存信息
            HxStorageStock storageStock = getById(hxStorageStockParam.getId());
            //进行相关校验
            String kcNumParam = hxStorageStockParam.getKcNum();
            String kcNumStock = storageStock.getKcNum();

            try {
                // 将字符串转换为浮点数
                double kcNumParamDouble = Double.parseDouble(kcNumParam);
                double kcNumStockDouble = Double.parseDouble(kcNumStock);

                // 将浮点数转换为整数（取整）
                int kcNumParamInt = (int) Math.round(kcNumParamDouble);
                int kcNumStockInt = (int) Math.round(kcNumStockDouble);


                // 进行比较
                if (kcNumParamInt > kcNumStockInt) {
                    return ResponseData.error("修改的数量大于库存数量");
                }
                //在此进行判断生产日期是否一致 detailed_date
                if (hxStorageStockParam.getDetailedDate().equals(storageStock.getDetailedDate())) {
                    //如果一致，必须调整全部数量
                    if (kcNumParamInt != kcNumStockInt) {
                        return ResponseData.error(500,"调整全部同一日期数量",null);
                    }
                    HxStorageStock stock = new HxStorageStock();
                    BeanUtils.copyProperties(hxStorageStockParam, stock);
                    storageStock.setDetailedDate(hxStorageStockParam.getDetailedDate());
                    // 更新库存信息
                    this.baseMapper.updateById(storageStock);
                } else {
                    if (kcNumParamInt == kcNumStockInt) {
                        HxStorageStock stock = new HxStorageStock();
                        BeanUtils.copyProperties(hxStorageStockParam, stock);
                        stock.setDetailedDate(hxStorageStockParam.getDetailedDate());
                        // 更新库存信息
                        this.baseMapper.updateById(stock);
                    }
                    if (kcNumParamInt < kcNumStockInt){
                    //如果不一致就要新增一条记录 并调整库存数量
                    HxStorageStock  newStocks= new HxStorageStock();
                    BeanUtils.copyProperties(storageStock, newStocks);
                    //传入的数量
                    newStocks.setKcNum(kcNumParam);
                    newStocks.setJibenNum(new BigDecimal(kcNumParam).multiply(new BigDecimal(storageStock.getKcRate())));
                    //传入的日期
                    newStocks.setStockVolume(new BigDecimal(storageStock.getGoodsVolume()).multiply(new BigDecimal(kcNumParam)).toString());
                    newStocks.setStockWeight(new BigDecimal(storageStock.getGoodsWeight()).multiply(new BigDecimal(kcNumParam)).toString());
                    newStocks.setStockMoney(new BigDecimal(storageStock.getGoodsPrice()).multiply(new BigDecimal(kcNumParam)).toString());

                    newStocks.setDetailedDate(hxStorageStockParam.getDetailedDate());
                    //将id置空
                    newStocks.setId(null);
                    //新增一条记录
                    this.save(newStocks);
                    //调整原库存数量
                    String formattedKcNum = String.format("%.3f", (double) (kcNumStockInt - kcNumParamInt));
                    storageStock.setKcNum(formattedKcNum);
                    storageStock.setJibenNum(new BigDecimal(formattedKcNum).multiply(new BigDecimal(storageStock.getKcRate())));
                    storageStock.setStockVolume(new BigDecimal(storageStock.getGoodsVolume()).multiply(new BigDecimal(formattedKcNum)).toString());
                    storageStock.setStockWeight(new BigDecimal(storageStock.getGoodsWeight()).multiply(new BigDecimal(formattedKcNum)).toString());
                    storageStock.setStockMoney(new BigDecimal(storageStock.getGoodsPrice()).multiply(new BigDecimal(formattedKcNum)).toString());

                    // 更新库存信息
                    this.baseMapper.updateById(storageStock);
                    }
                }
                return ResponseData.success(200, "修改成功", null);
            } catch (NumberFormatException e) {
                // 处理转换失败的情况
                e.printStackTrace();
                return ResponseData.error(500, "库存数量格式不正确",null);
            }

        }
        return ResponseData.error(500,"修改失败",null);
    }

}
